package com.hopu.blb_erp.service.impl;

import com.hopu.blb_erp.entity.LaserSecurity;
import com.hopu.blb_erp.entity.Product;
import com.hopu.blb_erp.entity.User;
import com.hopu.blb_erp.mapper.ProductMapper;
import com.hopu.blb_erp.mapper.UserMapper;
import com.hopu.blb_erp.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author A-Zhang
 * @since 2021-11-24
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;

    @Transactional
    @Override
    public LaserSecurity findLaserSecurity(String code) {
        LaserSecurity laserSecurity = productMapper.selectLaserSecurity(code);
        if (laserSecurity == null){
            return null;
        }
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User byName = userMapper.selectByName(user.getUsername());
        byName.setQueryTimes(byName.getQueryTimes() + 1);
        userMapper.updateById(byName);

        laserSecurity.setQueryNumber(byName.getQueryTimes() + 1);
        return laserSecurity;
    }

    @Override
    public List<Product> find(String pName) {
        return productMapper.selectAll(pName);
    }
}
